package com.comprehensive.personalmovie.controller;

import com.comprehensive.common.dto.MovieCmsPamas;
import com.comprehensive.common.dto.PageResponse;
import com.comprehensive.common.enumType.DataTypeEnum;
import com.comprehensive.common.po.MovieEpisodes;
import com.comprehensive.common.po.MovieSeasons;
import com.comprehensive.common.po.Movies;
import com.comprehensive.personalmovie.service.MovieService;
import com.comprehensive.personalmovie.service.VideoCrawlerService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/movie")
public class MovieController {
    private final MovieService movieService;
    private final VideoCrawlerService videoCrawlerService;
    @Autowired
    public MovieController(MovieService movieService, VideoCrawlerService videoCrawlerService) {
        this.movieService = movieService;
        this.videoCrawlerService = videoCrawlerService;
    }
    @GetMapping("/listPage")
    public ResponseEntity<PageResponse<Movies>>listPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(defaultValue = "0") int type,
            @RequestParam(defaultValue = "") String name){
        PageResponse<Movies> pageResponse = movieService.listPage(pageNum, pageSize,type,name);
        if (StringUtils.isNotBlank(name)){
            MovieCmsPamas movieCmsPamas = new MovieCmsPamas();
            movieCmsPamas.setPageNum(String.valueOf(pageNum));
            if (type != 0){
                movieCmsPamas.setType(DataTypeEnum.fromValue(type));
            }
            movieCmsPamas.setWd(name);
            List<Movies> movies = videoCrawlerService.crawlMovie(movieCmsPamas);
            pageResponse.getRecords().addAll(movies);
        }
        return ResponseEntity.ok(pageResponse);
    }
    @GetMapping("/cms/{id}")
    public ResponseEntity<Object> getMovieCMSById(@PathVariable("id") long id) throws JsonProcessingException {
        MovieCmsPamas movieCmsPamas = new MovieCmsPamas();
        movieCmsPamas.setIds(String.valueOf(id));
        return ResponseEntity.ok(videoCrawlerService.crawlMovieDetail(movieCmsPamas));
    }
    @GetMapping("/{id}")
    public ResponseEntity<Movies> getMovieById(@PathVariable("id") long id){
        Movies movie = movieService.getMovieById(id);
        return ResponseEntity.ok(movie);
    }
    @PostMapping
    public ResponseEntity<Boolean> addMovie(@RequestBody Movies movie){
        boolean result = movieService.addMovie(movie);
        return ResponseEntity.ok(result);
    }
    @PutMapping
    public ResponseEntity<Boolean> updateMovie(@RequestBody Movies movie){
        boolean result = movieService.updateMovie(movie);
        return ResponseEntity.ok(result);
    }
    @DeleteMapping("/{id}")
    public ResponseEntity<Boolean> deleteMovie(@PathVariable("id") long id){
        boolean result = movieService.deleteMovie(id);
        return ResponseEntity.ok(result);
    }
    @GetMapping("/seasons/{id}")
    public ResponseEntity<List<MovieSeasons>> getSeasonsByMovieId(@PathVariable("id") long id){
        List<MovieSeasons> seasons = movieService.getMovieSeasons(id);
        return ResponseEntity.ok(seasons);
    }
    @PostMapping("/seasons")
    public ResponseEntity<Boolean> addMovieSeasons(@RequestBody MovieSeasons movieSeasons){
        boolean result = movieService.addMovieSeasons(movieSeasons);
        return ResponseEntity.ok(result);
    }
    @PutMapping("/seasons")
    public ResponseEntity<Boolean> updateMovieSeasons(@RequestBody MovieSeasons movieSeasons){
        boolean result = movieService.updateMovieSeasons(movieSeasons);
        return ResponseEntity.ok(result);
    }
    @DeleteMapping("/seasons/{id}")
    public ResponseEntity<Boolean> deleteMovieSeasons(@PathVariable("id") long id){
        boolean result = movieService.deleteMovieSeasons(id);
        return ResponseEntity.ok(result);
    }
    @GetMapping("/episodesOnly/{id}")
    public ResponseEntity<MovieEpisodes> getEpisodesById(@PathVariable("id") long id){
        MovieEpisodes episodes = movieService.getMovieEpisodesById(id);
        return ResponseEntity.ok(episodes);
    }
    @GetMapping("/episodes/{id}")
    public ResponseEntity<List<MovieEpisodes>> getEpisodesBySeasonsId(@PathVariable("id") long id){
        List<MovieEpisodes> episodes = movieService.getMovieEpisodes(id);
        return ResponseEntity.ok(episodes);
    }
    @PostMapping("/episodes")
    public ResponseEntity<Boolean> addMovieEpisodes(@RequestBody MovieEpisodes movieEpisodes){
        boolean result = movieService.addMovieEpisodes(movieEpisodes);
        return ResponseEntity.ok(result);
    }
    @PutMapping("/episodes")
    public ResponseEntity<Boolean> updateMovieEpisodes(@RequestBody MovieEpisodes movieEpisodes){
        boolean result = movieService.updateMovieEpisodes(movieEpisodes);
        return ResponseEntity.ok(result);
    }
    @DeleteMapping("/episodes/{id}")
    public ResponseEntity<Boolean> deleteMovieEpisodes(@PathVariable("id") long id){
        boolean result = movieService.deleteMovieEpisodes(id);
        return ResponseEntity.ok(result);
    }
}
